/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jkoa.mgr;

import com.google.inject.Inject;
import com.jkoa.dao.PeribadiDAO;
import com.jkoa.model.Peribadi;
import com.jkoa.service.MajikanService;
import com.wideplay.warp.persist.Transactional;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;


/**
 *
 * @author fikri
 */
public class PekerjaManager {
    
    @Inject PeribadiDAO peribadiDAO;   
    
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    
    @Transactional
    public Peribadi savePeribadi(Peribadi peribadi) {
        return peribadiDAO.saveOrUpdate(peribadi);
    }
    
    public Peribadi findById(String id){
        return peribadiDAO.findById(Integer.parseInt(id));
    }
    
    
     public List<Peribadi> findAll(Map<String, String[]> param) {
        String query = "SELECT a FROM com.jkoa.model.Peribadi a WHERE a.aktif = 'Y'";
        
        if (MajikanService.isNotBlank(param.get("peribadi.nama"))) {
            query += " AND a.nama LIKE :nama ";
        }
        if (MajikanService.isNotBlank(param.get("peribadi.kodPengenalan"))) {
            query += " AND a.kodPengenalan = :kod";
        }
        
        if (MajikanService.isNotBlank(param.get("peribadi.noPengenalan"))) {
            query += " AND a.noPengenalan = :noPengenalan";
        }
        
        
        Query q = sessionProvider.get().createQuery(query);
        if (MajikanService.isNotBlank(param.get("peribadi.nama"))) {
            q.setString("nama", param.get("peribadi.nama")[0].trim()+"%");
        }
        
        if (MajikanService.isNotBlank(param.get("peribadi.kodPengenalan"))) {
            q.setString("kod", param.get("peribadi.kodPengenalan")[0].trim());
        }
        
        if (MajikanService.isNotBlank(param.get("peribadi.noPengenalan"))) {
            q.setString("noPengenalan", param.get("peribadi.noPengenalan")[0].trim());
        }
        return q.list();
    }
    
    
}
